package org.hyf.inspur.LessonDesin.clear.count8;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;
public class PreUsersReducer extends Reducer<Text, IntWritable, NullWritable,Text> {
    protected void reduce(Text key, Iterable<IntWritable> values,
                          Reducer<Text, IntWritable, NullWritable, Text>.Context context)
            throws IOException, InterruptedException {
        Iterator<IntWritable> it = values.iterator();
        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        String str1 = "";
        String str2 = "";
        String str = "";
        while (it.hasNext()) {
            IntWritable value = it.next();
            int num = value.get();
            if (num == 1) {
                str1 = "1";
                count1 = count1 + 1;
            }
            if (num == 2) {
                str2 = "2";
                count2 = count2 + 1;
            }
            if (num == 0) {
                count3 = count3 + 1;
            }
        }
        if (count1 == 0) {
            str = key.toString() + "|" + str2 + ":" + count2;
        } else if (count2 == 0) {
            str = key.toString() + "|" + str1 + ":" + count1;
        } else {
            str = key.toString() + "|" + str1 + ":" + count1 + "," + str2 + ":" + count2;
        }
        //str = key.toString() + "|" + str1 + ":" + count1 + "," +  str2 + ":" + count2;
        context.write(NullWritable.get(), new Text(str));
    }
}
